// blessing/pages/game/game.js
var app = getApp();
var loading = false;
Page({
	onShareAppMessage:function(){
	  var that = this;
	  var sharedata = that.data.shareData;
	  return {
		  title: sharedata.title,
	      path: sharedata.path,
	      imageUrl:sharedata.pic,
	      success(res){
	    	  var shareTicket = res.shareTickets[0] // 获取 shareTicket
	    	  wx.getShareInfo({
	              shareTicket: shareTicket,
	              complete(res) {
	            	  app.util.request({
		    	        url: 'entry/wxapp/addnums',
		    	        data: {
		    	            m : 'junsion_challeng',
		    	            t : 1,
		    	            mid : that.data.openid,
		    	            iv : res.iv,
		    	            encryptedData : res.encryptedData,
		    	        },
		    	        success: function (res) {
		    	        	if (res.data.code == 1) {
	                        	wx.showToast({
	                        		title:'次数\t+'+res.data.num,
	                        		icon:'none',
	                				mask:true,
	                				duration:1000
	                			});
		    	    	    }
		    	        	else{
		    	        		 wx.showModal({title:res.data.msg,showCancel:false});
		    	        	}
		    	        }
		    	    }); 
	                
	              }
	          });
	      },
	      fail: function(res) {
	        // 转发失败
	    	  console.log(res);
	      }
	  }
  },
  data: {
	  type:0,
	  openid:'',
	  games:{},
	  collect1:'',
	  collect:'',
	  list_bg:'',
	  footer:{},
	  currentPage:'',
	  rarrow:'',
	  liulan:'',
	  
	  scrollTop : 0,
	  scrollHeight : 0,
	  hasMore:true,
	  page:1,
  },
  onLoad: function (options) {
	  var that = this;
	  if(options.type){
		  var type = options.type;
		  that.setData({type:type});
	  }else{
		  var type = 0;
	  }
	  wx.showLoading({title:'数据加载中...'});
	  app.sysInit(this,function(res){
		  app.getUserInfo(function (res) {
	          var openid = res['openid'];
	          that.setData({openid:openid});
	        //获取分享内容
	          var gametype = 0;
	          var sysType = wx.getStorageSync('gameType');
	          if(parseInt(sysType)>=0){
          		  gametype = sysType;
          	  }
              app.getShareData(function(res){
              	that.setData({shareData:res.data});
              },openid,gametype);
	          app.util.request({
	              url: 'entry/wxapp/game',
	              data: {
	                m: 'junsion_challeng',
	                t: 1,
	                openid:openid,
	                type:type
	              },
	              success: function (res) {
	                wx.hideLoading();
	            	that.setData({
	            		games : res.data.games,
	            		liulan : res.data.liulan,
	            		rarrow : res.data.rarrow,
	            		page:1,
	            		hasMore:true
	                })
	                loading = false;
	             }
	         })
		  });    
	  });    
     wx.getSystemInfo({
         success:function(res){
             that.setData({
                 scrollHeight:res.windowHeight
             });
         }
     });
  },
  goGame:function(e){
	  var that = this;
	  var dataset = e.currentTarget.dataset;
	  var index = dataset.index;
	  var id = dataset.id;
	  var game = that.data.games,
	  game = game[index];
	  app.util.request({
          url: 'entry/wxapp/addGNums',
          data: {
            m: 'junsion_challeng',
            t: 1,
            id:id,
          },
          success: function (res) {
        	  if(game.appid){
        		  wx.navigateToMiniProgram({
          	        appId: game.appid,
          	        path: game.url,
                  });
          	  	  return;
        	  }
          	  else if(game.url.indexOf('http') >= 0){
          		  wx.setStorageSync('advurl', game.url);
          	      wx.navigateTo({
          	         url: '../web/web',
          	      })
          	  }else{
	              wx.reLaunch({
	                url: game.url,
	              })
	          }	
         }
     })
  },
  scroll : function(e){
    if(e.detail.scrollTop > 100){
      this.setData({
            floorstatus: true
      });
      }else {
      this.setData({
          floorstatus: false
      });
      }
  },
  //加载更多
  loadMore: function(e) {
    var that = this;
    if (loading) return;
    if (!that.data.hasMore) return;
    loading = true;
    wx.showLoading();
    app.util.request({
        url: 'entry/wxapp/game',
        data: {
            m : 'junsion_challeng',
            t : 1,
            scroll:true,
            page:++that.data.page,
            type:that.data.type,
        },
        success: function (res) {
          wx.hideLoading();
          if(res.data.status == 1){
            loading = false;
            that.setData({
            	games: that.data.games.concat(res.data.games),
            });
          }else{
            that.setData({
              hasMore: false,
            });
          }
        },
    });
  },  
 
})